$(function(){
	tv_index.run();
});

var tv_index = {
		run: function(){
			this._init_();
			this._start_();
			this._bind_events();
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val() || "";
			this.scrollPage = 1;			
			this.itemUrl = '';
			this.title = '';
			this.initial = $("#alpha").val() || "";
			this.genre = $("#genre").val() || "";
			this.decade = $("#decade").val() || "";
			
			this.vdInstalled = vd.check();			
		},
		
		_start_: function(){						
			side_pub._init_side_genre_decade(this);//写在main.js中
			
			this.location = location.href;				
			var index = this.location.indexOf("#");					
			var params = null;
			if(index >= 0){
				var loc = this.location;
				this.location = loc.substring(0, index);
				params = loc.substring(index + 1);				
			}
			
			if(params){
				this._query_tv_shows(params);
			}else{
				this._add_content();
			}		
		},
		
		_bind_events: function(){
			$("#genre_div > div.vscroll-content > ul > li > a").click(function(){
				var $this = $(this);
				var genreTemp = $this.attr("genre");
				$("#genre_div > div.vscroll-content > ul > li > a").removeAttr("class");
				$this.addClass("selected");
				$("#alpha_div").find("a").removeClass("selected");
				$("#alpha_div > a").first().addClass("selected");
				
				if(genreTemp){
					tv_index.genre = genreTemp;
					tv_index.initial = null;
					tv_index.sort = null;
				}else{
					tv_index.genre = null;
					tv_index.initial = null;
					tv_index.sort = null;
				}
				
				tv_index.scrollPage = 1;//改变基因将scrollPage恢复为1
				
				tv_index._change_location();
				tv_index._query_tv_shows();
				return false;
			});			
			
			$("#decade_div > ul > li > a").click(function(){
				var $this = $(this);
				var decadeTemp = $this.text();
				$("#decade_div > ul > li > a").removeAttr("class");
				$this.addClass("selected");
				$("#alpha_div").find("a").removeClass("selected");
				$("#alpha_div > a").first().addClass("selected");
				
				if('All Decades' == decadeTemp){
					tv_index.decade = null;
					tv_index.initial = null;
					tv_index.sort = null;
				}else{
					tv_index.decade = decadeTemp.substring(0,4);
					tv_index.initial = null;
					tv_index.sort = null;
				}
				
				tv_index.scrollPage = 1;
				tv_index._change_location();
				tv_index._query_tv_shows();
				return false;
			});
			
			$("#alpha_div > a.initial").click(function(){
				var $this = $(this);
				var temp = $this.text();
				$("#alpha_div > a.initial").removeClass("selected");
				$this.addClass("selected");
				
				if("Popular" == temp){
					tv_index.sort = "popular";
					tv_index.initial = null;
				}else if("Time" == temp){
					tv_index.sort = "time";
					tv_index.initial = null;
				}else{
					tv_index.initial = $this.text();
					tv_index.sort = "popular";
				}
				
				tv_index.scrollPage = 1;
				tv_index._change_location();
				tv_index._query_tv_shows();
				return false;
			});
			
			tv_index._scroll_add_content();
			tv_index._set_location_value();
			//tv_index._bind_tips_event();
		},
		
		_change_location: function(){
			var params = [];
			if(this.genre){
				params.push("genre=" + this.genre);
			}
			if(this.decade){
				params.push("decade=" + this.decade);
			}
			if(this.initial){
				params.push("initial=" + this.initial);
			}
			if(params.length > 0){
				params = params.join("&");
			}else{
				params = "";
			}
			
			location.href = this.location + "#" + params;
		},
			
		_query_tv_shows: function(params){			
			var url = this.contextPath + "/tv/full-length/query";
			var data = null;
			if(params){
				data = params;
			}else{
				data = this._gather_params();
			}
			
			$.get(url, data, function(result){
				$("#show_content_div").html(result);
				//重新绑定事件
				tv_index._set_location_value();
				tv_index._add_content();
			});
		},
		
		_set_location_value: function(){			
			$("#show_content_div > div.mi_box").find("a").click(function(){
				var href = $(this).attr("href");
				var data = [];
				if($(this).text() == "Download"){
					return ;
				}
				if(tv_index.genre){
					data.push("genre=" + tv_index.genre);
				}
				if(tv_index.decade){
					data.push("decade=" + tv_index.decade);
				}
				location.href = href + "#" + data.join("&");
				return false;
			});
		},
		
		_gather_params: function(){
			var params = [];
			if(this.genre){
				params.push("genre=" + this.genre);
			}
			if(this.decade){
				params.push("decade=" + this.decade);
			}
			if(this.initial){
				params.push("initial=" + this.initial);
			}
			if(this.sort){
				params.push("sort=" + this.sort);
			}
			if(params.length > 0){
				return params.join("&");
			}
		},
		
		_scroll_add_content: function(){
		    $(window).scroll(function () {
		    	var winH = $(window).height(); //页面可视区域高度
				var pageH = $(document).height(); 
	        	var scrollT = $(window).scrollTop(); //滚动条top 
	        	var aa = pageH-winH-scrollT;
				if(aa<0.02){
		        	tv_index._add_content();
				}
		    });
		},
		
		_add_content: function(){
	        if($("#pageCount").val() > 1 && tv_index.scrollPage < $("#pageCount").val()){
				tv_index.scrollPage += 1;
				var data = this._gather_params(); 
				if(data){
					data += "&page=" + tv_index.scrollPage;
				}else{
					data ="page=" + tv_index.scrollPage;
				}
				var url = main.contextPath + "/tv/full-length/query";
				$.ajax({
					url: url,
					data: data,
					cache: false,
					beforeSend: function(){
						$(".loadingBox").show();
					},
					success: function(result){
						$("#show_content_div").append(result);
						$(".loadingBox").hide();
						tv_index._set_location_value();
					}
				});
	        }
	        if($("#pageCount").val() == tv_index.scrollPage){
	        	$("#show_content_div").append("<div class='clear'/>");
	        }
		}
};